package com.allen.flink.batch;

import org.apache.commons.io.FileUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能: 分布式缓存
 *
 * @date: 2020-03-24 14:16
 * @author: Allen
 * @version: 0.0.4-snapshot
 * @Email: allenZyhang@163.com
 * @since: JDK 1.8
 **/
public class DistributeCache {
    
    public static void main(String[] args) throws Exception {
        //获取执行环境
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        
        //注册一个本地可执行文件  用户基本信息字典
        env.registerCachedFile("D:/Flink/user.txt", "user", true);
        
        //准备用户游戏充值数据
        ArrayList<Tuple2<String, Integer>> data = new ArrayList<>();
        data.add(new Tuple2<>("101", 2000));
        data.add(new Tuple2<>("102", 1500));
        data.add(new Tuple2<>("103", 1000));
        
        //读取数据源
        DataSet<Tuple2<String, Integer>> ds = env.fromCollection(data);
        //转换为map
        DataSet<String> result = ds.map(new RichMapFunction<Tuple2<String, Integer>, String>() {
            
            Map<String, String> allMap = new HashMap<>();
            
            @Override
            public String map(final Tuple2<String, Integer> value) throws Exception {
                String userName = allMap.get(value.f0);
                return userName + "," + value.f1;
            }
            
            @Override
            public void open(final Configuration parameters) throws Exception {
                super.open(parameters);
                //获取分布式缓存
                File userInfo = this.getRuntimeContext().getDistributedCache().getFile("user");
                List<String> lines = FileUtils.readLines(userInfo);
                for (final String line : lines) {
                    String[] value = line.split(",");
                    allMap.put(value[0], value[1]);
                }
            }
        });
        
        result.print();
    }
    
}
